package com.telchina.workorder.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.telchina.common.core.domain.AjaxResult;
import com.telchina.common.domain.WoAttachment;
import com.telchina.common.mapper.WoAttachmentMapper;
import com.telchina.common.utils.DateUtils;
import com.telchina.workorder.domain.WoReply;
import com.telchina.workorder.domain.vo.WoReplyVO;
import com.telchina.workorder.mapper.WoReplyMapper;
import com.telchina.workorder.service.IWoReplyService;
import com.telchina.workorder.third.service.SystemThirdService;
import lombok.Synchronized;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 回复单Service业务层处理
 *
 * @author konghao
 * @date 2023-02-21
 */
@Service
public class WoReplyServiceImpl extends ServiceImpl<WoReplyMapper, WoReply> implements IWoReplyService {

    @Autowired
    private SystemThirdService systemThirdService;
    @Autowired
    private WoAttachmentMapper woAttachmentMapper;

    /**
     * 查询回复单
     *
     * @param id 回复单ID
     * @return 回复单
     */
    @Override
    public WoReply selectById(String id) {
        return baseMapper.selectById(id);
    }

    /**
     * 分页查询回复单列表
     *
     * @param woReply 回复单
     * @return 回复单
     */
    @Override
    public IPage<WoReply> selectList(IPage<WoReply> page, WoReply woReply) {
        return baseMapper.selectWoReplyPage(page, woReply);
    }

    /**
     * 查询所有回复单列表
     *
     * @param woReply 回复单
     * @return 回复单
     */
    @Override
    public List<WoReply> selectListAll(WoReply woReply) {
        return baseMapper.selectWoReplyList(woReply);
    }

    /**
     * 新增回复单
     *
     * @param woReply 回复单
     * @return 结果
     */
    @Override
    public AjaxResult insert(WoReply woReply) {
        woReply.setReplyTime(DateUtils.getNowDate()); // 回复时间
        woReply.setReplyNo(getReplayNumber()); // 回复单编号
        woReply.setDeptId(systemThirdService.getLoginUseDeptId());
        return saveOrUpdate(woReply) ? AjaxResult.success(woReply) : AjaxResult.error();
    }

    /**
     * 修改回复单
     *
     * @param woReply 回复单
     * @return 结果
     */
    @Override
    public int update(WoReply woReply) {
        woReply.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(woReply);
    }

    /**
     * 批量删除回复单
     *
     * @param ids 需要删除的回复单ID
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除回复单信息
     *
     * @param id 回复单ID
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return baseMapper.deleteById(id);
    }

    /**
     * 回复单列表
     *
     * @param woReply
     * @return
     */
    @Override
    public List<WoReplyVO> selectListAllAndAttachment(WoReply woReply) {
        List<WoReplyVO> listVO = new ArrayList<>();
        List<WoReply> list = this.selectListAll(woReply);
        // 获取对应的附件信息
        List<String> replyIds = list.stream().map(WoReply::getId).collect(Collectors.toList());
        List<WoAttachment> woAttachmentList = Collections.emptyList();
        if (CollUtil.isNotEmpty(replyIds)) {
            woAttachmentList = woAttachmentMapper.selectList(new QueryWrapper<WoAttachment>()
                    .lambda().in(WoAttachment::getBussId, replyIds));
        }
        List<WoAttachment> finalWoAttachmentList = woAttachmentList;
        list.stream().forEach(A -> {
            WoReplyVO woReplyVO = new WoReplyVO();
            BeanUtils.copyProperties(A, woReplyVO);
            woReplyVO.setWoAttachmentList(finalWoAttachmentList.stream().filter(B -> A.getId().equals(B.getBussId()))
                    .collect(Collectors.toList()));
            listVO.add(woReplyVO);
        });
        return listVO;
    }

    /**
     * 同步方法生成工单编号
     *
     * @return
     */
    @Synchronized
    private String getReplayNumber() {
        String paramsDate = DateUtils.parseDateToStr("yyMMdd", DateUtils.getNowDate());
        // 获取当日最大单号
        WoReply woReply = baseMapper.selectOne(new QueryWrapper<WoReply>().select("max(reply_no) reply_no")
                .likeRight("reply_no", paramsDate));
        if (woReply != null) {
            return String.valueOf(new BigDecimal(woReply.getReplyNo()).add(BigDecimal.ONE));
        } else {
            return paramsDate + "00001";
        }
    }
}
